package org.dromara.merchants.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.merchants.domain.TaCategory;
import org.dromara.merchants.domain.TaGoods;
import org.dromara.merchants.domain.TaSetMeal;
import org.dromara.merchants.domain.bo.TaCategoryBo;
import org.dromara.merchants.domain.vo.TaGoodsVo;
import org.dromara.merchants.domain.vo.TaSetMealVo;
import org.dromara.merchants.mapper.TaGoodsMapper;
import org.dromara.merchants.mapper.TaSetMealMapper;
import org.springframework.stereotype.Service;
import org.dromara.merchants.domain.bo.TaCustomClassificationBo;
import org.dromara.merchants.domain.vo.TaCustomClassificationVo;
import org.dromara.merchants.domain.TaCustomClassification;
import org.dromara.merchants.mapper.TaCustomClassificationMapper;
import org.dromara.merchants.service.ITaCustomClassificationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 特色分类Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class TaCustomClassificationServiceImpl implements ITaCustomClassificationService {

    private final TaCustomClassificationMapper baseMapper;
    private final TaGoodsMapper goodsMapper;
    private final TaSetMealMapper setMealMapper;

    /**
     * 查询特色分类
     */
    @Override
    public TaCustomClassificationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询特色分类列表
     */
    @Override
    public TableDataInfo<TaCustomClassificationVo> queryPageList(TaCustomClassificationBo bo, PageQuery pageQuery) {
        Page<TaCustomClassificationVo> result = baseMapper.selectCustomClassificationPage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * app端查询特色分类列表
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<TaCustomClassificationVo> queryAppList(TaCustomClassificationBo bo, PageQuery pageQuery) {
        Page<TaCustomClassificationVo> result = baseMapper.selectVoPage(pageQuery.build(), this.buildLambdaQueryWrapper(bo));
        return TableDataInfo.build(result);
    }

    /**
     * 查询特色分类列表
     */
    @Override
    public List<TaCustomClassificationVo> queryList(TaCustomClassificationBo bo) {
        return baseMapper.selectVoList(this.buildQueryWrapper(bo));
    }

    /**
     * 查询特色分类下拉列表
     *
     * @return
     */
    @Override
    public List<TaCustomClassificationVo> queryOptionList(TaCustomClassificationBo bo) {
        return baseMapper.selectCustomClassificationList(this.buildQueryWrapper(bo));
    }

    /*构建查询条件*/
    private Wrapper<TaCustomClassification> buildQueryWrapper(TaCustomClassificationBo bo) {
        QueryWrapper<TaCustomClassification> wrapper = Wrappers.query();
        wrapper.eq("cc.del_flag", "0")
            .like(StringUtils.isNotBlank(bo.getClassificationName()), "cc.classification_name", bo.getClassificationName())
            .eq(bo.getStoreId() != null, "cc.store_id", bo.getStoreId())
            .eq(StringUtils.isNotBlank(bo.getType()), "cc.type", bo.getType())
            .eq(StringUtils.isNotBlank(bo.getStatus()), "cc.status", bo.getStatus());
        return wrapper;
    }

    private LambdaQueryWrapper<TaCustomClassification> buildLambdaQueryWrapper(TaCustomClassificationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaCustomClassification> lqw = Wrappers.lambdaQuery();
        lqw.
            like(StringUtils.isNotBlank(bo.getClassificationName()), TaCustomClassification::getClassificationName, bo.getClassificationName())
            .eq(ObjectUtil.isNotNull(bo.getStoreId()), TaCustomClassification::getStoreId, bo.getStoreId())
            .eq(StringUtils.isNotBlank(bo.getType()), TaCustomClassification::getType, bo.getType())
            .eq(StringUtils.isNotBlank(bo.getStatus()), TaCustomClassification::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增特色分类
     */
    @Override
    public Boolean insertByBo(TaCustomClassificationBo bo) {
        TaCustomClassification add = MapstructUtils.convert(bo, TaCustomClassification.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClassificationId(add.getClassificationId());
        }
        return flag;
    }

    /**
     * 修改特色分类
     */
    @Override
    public Boolean updateByBo(TaCustomClassificationBo bo) {
        TaCustomClassification update = MapstructUtils.convert(bo, TaCustomClassification.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaCustomClassification entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除特色分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            /*查询在goods或者setMeal中是否有特色分类正在使用*/
            List<TaGoodsVo> goodsList = goodsMapper
                .selectVoList(Wrappers.<TaGoods>lambdaQuery().in(TaGoods::getClassificationId, ids));
            if (!goodsList.isEmpty()) {
                throw new ServiceException("该特色分类正在被商品使用，无法删除");
            }
            List<TaSetMealVo> setMealList = setMealMapper
                .selectVoList(Wrappers.<TaSetMeal>lambdaQuery().in(TaSetMeal::getClassificationId, ids));
            if (!setMealList.isEmpty()) {
                throw new ServiceException("该特色分类正在被套餐使用，无法删除");
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
